{extend name="common/form" /}
{block name="form" /}
<div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
    <div class="widget am-cf">
        <div class="widget-head am-cf">

            <div class="widget-title am-fl">添加广告</div>
            <div class="widget-function am-fr">
                <a href="javascript:;" class="am-icon-cog"></a>
            </div>
        </div>
        <div class="widget-body am-fr">
            <form class="am-form tpl-form-border-form tpl-form-border-br">
                <input type="hidden" name="id" value="{$info.id|default=''}">


                <div class="am-form-group  am-form-file">
                    <label for="user-name" class="am-u-sm-3 am-form-label">
                        图片
                    </label>
                    <div class="am-u-sm-9">
                        <i class="am-icon-camera am-icon-btn"></i>
                        <p>请选择图片..</p>

                        <input type="file" value="{$info.url|default=''}" multiple accept="image/*" onchange="up(this)" id="photoFile">
                        <img style="width: 200px" id="images" src="{$info.url|default=''}">
                    </div>
                </div>
                <div class="am-form-group  am-form-file">
                    <label for="user-name" class="am-u-sm-3 am-form-label">
                        图片地址
                    </label>
                    <div class="am-u-sm-9">
                       <input type="text" name="url" value="{$info.url|default=''}" required="required" class="tpl-form-input" placeholder="请上传">
                    </div>
                </div>
                <div class="am-form-group">
                    <label for="user-name" class="am-u-sm-3 am-form-label">
                        类型 </label>
                    <div class="am-u-sm-9">
                        <select name="type_id" required="required" data-am-selected="{searchBox: 2}">
                            {foreach $type_list as $v}
                            <option value="{$v.id}" {present name='$info.type_id' } {eq name="$info.type_id"  value="$v.id"}selected{/eq}{else/} {eq name="$Request.param.type_id"  value="$v.id"}selected{/eq}{/present}>{$v.type_name}</option>
                            {/foreach}
                        </select>
                    </div>
                </div>
                <div class="am-form-group">
                    <label for="user-name" class="am-u-sm-3 am-form-label">
                        标题
                    </label>
                    <div class="am-u-sm-9">
                        <input type="text" name="title" value="{$info.title|default=''}" required="required"
                               class="tpl-form-input" placeholder="请填写">
                    </div>
                </div>
                <div class="am-form-group">
                    <label for="user-name" class="am-u-sm-3 am-form-label">
                        跳转链接 </label>
                    <div class="am-u-sm-9">
                        <input type="text" name="link" value="{$info.link|default=''}" class="tpl-form-input" placeholder="请填写跳转链接">
                    </div>
                </div>

                <div class="am-form-group">
                    <label for="user-name" class="am-u-sm-3 am-form-label">
                        是否显示
                    </label>
                    <div class="am-u-sm-9">
                        <div class="tpl-switch">
                            <input type="checkbox" name="is_show"  class="ios-switch bigswitch tpl-switch-btn am-margin-top-xs" {present name='$info.is_show'}{eq name='$info.is_show' value="1"}checked{/eq}{else /} checked{/present}>
                            <div class="tpl-switch-btn-view">
                                <div></div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="am-form-group">
                    <div class="am-u-sm-9 am-u-sm-push-3">
                        <button type="button" onclick="Updata();"
                                class="am-btn am-btn-primary tpl-btn-bg-color-success">
                            提交
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<div class="am-modal am-modal-alert" tabindex="-1" id="my-alert">
    <div class="am-modal-dialog">
        <div class="am-modal-bd">
        </div>
        <div class="am-modal-footer">
            <span class="am-modal-btn">确定</span>
        </div>
    </div>
</div>
{/block}
{block name='script' /}
<script type="text/javascript">
    function up(e) {
        var target = $(e);
        var file;
        if(target[0].files && target[0].files[0] ) {
            file = target[0].files[0];
        }
        if(file) {
            var reader = new FileReader();
            reader.onload = function(evt){
                var imgstr = evt.target.result; //这就是base64字符串
                document.getElementById('images').setAttribute('src',imgstr);
            };
            reader.readAsDataURL(file);
        }
    }
    function Updata() {
        var target = $("#photoFile");
        var file;
        if(target[0].files && target[0].files[0] ) {
            file = target[0].files[0];
        }

        if(!$('input[name="title"]').val()){
            $('.am-modal-bd').html("标题不能为空！");
            $('#my-alert').modal({
                relatedElement: this,
            })
            return;
        }
        if(!file  && !$('input[name="url"]').val()){
            $('.am-modal-bd').html("请选择图片！");
            $('#my-alert').modal({
                relatedElement: this,
            })
            return;
        }

        if(file){
            var index = file.name.lastIndexOf(".");
            var suffix = file.name.substr(index+1);
            var keys = Math.random().toString().slice(-10)+"."+suffix;
            var formData = new FormData();
            formData.append('upload',document.getElementById('photoFile').files[0]);
            $.AMUI.progress.start();
            $.ajax({
                url: "{:url('/Up/upImage')}",
                type: 'POST',
                data: formData,
                dataType: 'JSON',
                cache: false,
                processData: false,
                contentType: false
            }).done(function(ret) {
                if(ret.code ==0){
                    $('input[name="url"]').val(ret.data);
                    add('{:url($type)}','form')
                    $.AMUI.progress.done();
                }else{
                    $('.am-modal-bd').html(ret.msg)
                    $('#my-alert').modal({
                        relatedElement: this,
                    })
                }


            }).error(function (xhr) {
                $('.am-modal-bd').html(xhr.responseJSON.error)
                $('#my-alert').modal({
                    relatedElement: this
                })
            })
        }else{
            add('{:url($type)}','form')
        }

    }
</script>
{/block}
{extend}
